﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.SharePoint;

namespace ProjectHT.YKDGVB2012.Core.Datacontract
{
    public class MainServices : IMainList
    {
        private SPSite osite;
        private SPWeb oweb;
        public MainServices(SPContext context)
        {
            osite = context.Site;
            oweb = osite.OpenWeb();
        }
        public void CreateMainList(Mainlist mainlist)
        {
            if (oweb != null)
            {
                try
                {
                    SPSecurity.RunWithElevatedPrivileges(delegate
                                                             {
                                                                 oweb.AllowUnsafeUpdates = true;
                                                                 var olist = oweb.Lists[Common.MainList.MAINLIST];
                                                                 if (olist != null)
                                                                 {
                                                                     var item = olist.Items.Add();
                                                                     item[Common.MainList.FIELDTITLE] = mainlist.Title;
                                                                     item[Common.MainList.FIELDDESCRIPTION] = mainlist.Description;
                                                                     item[Common.MainList.FIELDSTATUS] = mainlist.Status;
                                                                     item[Common.MainList.FIELDEXPIRE] = mainlist.Expires;
                                                                     item.Update();
                                                                 }
                                                                 if (olist != null) olist.Update();
                                                                 oweb.AllowUnsafeUpdates = false;
                                                             });
                }
                catch (SPException ex)
                {
                    Helper.SystemWriteLog(ex.Message);
                }
            }
        }

        public List<SPListItem> GetListAll()
        {
            var datalist = new List<SPListItem>();
            if (oweb != null)
            {
                try
                {
                    SPSecurity.RunWithElevatedPrivileges(delegate
                                                             {
                                                                 var olist = oweb.Lists[Common.MainList.MAINLIST];
                                                                 datalist.AddRange(olist.Items.Cast<SPListItem>());
                                                             });
                }
                catch (SPException ex)
                {
                    Helper.SystemWriteLog(ex.Message);
                }
            }
            return datalist;
        }

        public SPListItem GetbyId(int itemId)
        {
            return GetListAll().Where(p => p.ID == itemId).FirstOrDefault();
        }

        public void DeleteItem(int itemId, SPContext context)
        {

            var spweb = context.Web;
                      
            SPSecurity.RunWithElevatedPrivileges(delegate
                                                     {
                                                         spweb.AllowUnsafeUpdates = true;
                                                         var item = GetbyId(itemId);                                                         
                                                         if (item != null)
                                                         {
                                                             item.Delete();
                                                         }
                                                         spweb.AllowUnsafeUpdates = false;

                                                     });


        }

        public void UpdateItem(Mainlist mainlist)
        {

        }
    }
}
